﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsMeasureTemplateRepository : BaseRepository<MsMeasureTemplate>, IMsMeasureTemplateRepository
    {
        /// <summary>
        /// 根据户型图Id获取测量项模板信息
        /// </summary>
        /// <param name="houseMapId">户型图编号</param>
        /// <returns></returns>
        public async Task<List<MsMeasureTemplateDto>> GetMeasureTemplate(Guid houseMapId)
        {
            var result = await (
              from h in Db.MsHouseMaps
              join j in Db.MsMeasureTemplates on h.HouseMapId equals j.HouseMapId
              where !j.IsDeleted && h.Status == MsHouseMap.EnumHouseMapStatus.启用
                    && j.HouseMapId == houseMapId
              select new MsMeasureTemplateDto()
              {
                  HouseMapId = h.HouseMapId,
                  Title = j.Title,
                  Id = j.Id,
                  DisplayValue = $"{j.Id}:",
                  Type = j.Type ?? (j.Code == "r-kaijianjinshenjicha" ? 1 : 0),
                  Code = j.Code,
                  DesignValues = j.DesignValues.ToObject<List<DesignValue>>()
              })
              .OrderBy(x => x.Type)
              .ThenBy(x => x.Title)
              .ToListAsync();

            if (result != null)
            {
                result = result.OrderBy(z => int.Parse(z.Id.Substring(1))).ToList();
                foreach (var mm in result)
                {
                    string value = string.Empty;
                    foreach (var des in mm.DesignValues)
                    {
                        value += $"{des.Title}:{(int)des.Value},";
                    }
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        mm.DisplayValue += value.Substring(0, value.Length - 1);
                    }
                }
            }
            return result;
        }

        ///// <summary>
        ///// 根据户型图Id获取测量项模板信息
        ///// </summary>
        ///// <param name="houseMapId">户型图编号</param>
        ///// <returns></returns>
        //public async Task<List<MsMeasureTemplate>> GetMeasureTemplate(Guid houseMapId)
        //{
        //    return await (
        //      from h in Db.MsHouseMaps
        //      join j in Db.MsMeasureTemplates on h.HouseMapId equals j.HouseMapId
        //      where !h.IsDeleted && !j.IsDeleted && h.Status == MsHouseMap.EnumHouseMapStatus.启用
        //            && j.HouseMapId == houseMapId
        //      select new MsMeasureTemplate()
        //          ).ToListAsync();

        //}

        /// <summary>
        /// 根据户型图和站点获取人工测量项
        /// </summary>
        /// <param name="houseMapId">户型图编号</param>
        /// <param name="pointId">站点编号</param>
        /// <returns></returns>
        public async Task<List<MeasureItem>> GetManualMeasureItem(Guid houseMapId, Guid pointId)
        {
            return await (
              from h in Db.MsHouseMaps
              join x in Db.MsHouseMapPoints on h.HouseMapId equals x.HouseMapId
              join i in Db.MsMeasureItems on new { HouseMapId = x.HouseMapId, PointId = x.PointId } equals new { HouseMapId = i.HouseMapId, PointId = i.PointId }
              join j in Db.MsMeasureTemplates on new { HouseMapId = i.HouseMapId, PointId = i.Id } equals new { HouseMapId = j.HouseMapId, PointId = j.Id }
              where !j.IsDeleted && h.Status == MsHouseMap.EnumHouseMapStatus.启用
                      && j.HouseMapId == houseMapId && x.PointId == pointId
              select new MeasureItem()
              {
                  HouseMapId = h.HouseMapId.ToString(),
                  PointId = x.PointId.ToString(),
                  Title = j.Title,
                  Id = j.Id,
                  Code = j.Code,
                  Desc = string.Empty,
                  DesignValues = j.DesignValues.ToObject<List<DesignValue>>()
              }).ToListAsync();
        }

        public async Task<List<MsMeasureTemplate>> GetDataByhouseMapIds(Guid[] houseMapIds)
        {
            return await (
             from h in Db.MsMeasureTemplates
             where houseMapIds.Contains(h.HouseMapId)
             select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MsMeasureTemplate>> GetList(Guid houseMapId)
        {
            return await (
              from h in Db.MsHouseMaps
              join j in Db.MsMeasureTemplates on h.HouseMapId equals j.HouseMapId
              where !j.IsDeleted && h.Status == MsHouseMap.EnumHouseMapStatus.启用
                    && j.HouseMapId == houseMapId
              select j).ToListAsync();
        }
    }
}